package org.xinjiang.data.migration.flush.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.data.migration.common.response.Param;
import org.xinjiang.data.migration.flush.convert.DcpgFlushConvert;
import org.xinjiang.data.migration.flush.dto.res.FlushResultDTO;
import org.xinjiang.data.migration.flush.service.IJDcpgFlushService;
import org.xinjiang.data.migration.hainan.convert.DcpgConvert;
import org.xinjiang.data.migration.hainan.convert.FlushLogConvert;
import org.xinjiang.data.migration.hainan.entity.DcpgEntity;
import org.xinjiang.data.migration.hainan.service.IDcpgService;
import org.xinjiang.data.migration.hainan.service.IFlushLogService;
import org.xinjiang.data.migration.xinjiang.dto.req.XjDcpgQueryDTO;
import org.xinjiang.data.migration.xinjiang.dto.res.XjSubDcpgResultDTO;
import org.xinjiang.data.migration.xinjiang.service.IXjDcpgService;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DcpgFlushServiceImpl implements IJDcpgFlushService {
    //新疆的service(oracle数据库)
    @Autowired
    private IXjDcpgService xjDcpgService;

    //海南的service(达梦数据库)
    @Autowired
    private IDcpgService dcpgService;

    @Autowired
    private IFlushLogService flushLogService;

    @Autowired
    private FlushLogConvert flushLogConvert;

    @Autowired
    private DcpgFlushConvert dcpgFlushConvert;

    @Autowired
    private DcpgConvert dcpgConvert;

    @Override
    public FlushResultDTO flush(Param pageable, XjDcpgQueryDTO dto) {
        FlushResultDTO result = new FlushResultDTO();
        IPage<XjSubDcpgResultDTO> pageList = xjDcpgService.findPageList(pageable, dto);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return result;
        }

        List<DcpgEntity> insertList = pageList.getRecords().stream().map(dcpgFlushConvert::convert).map(dcpgConvert::create).collect(Collectors.toList());
        this.processPageData(insertList, result);
        return result;
    }

    @Override
    public FlushResultDTO flushAll(XjDcpgQueryDTO dto) {
        FlushResultDTO result = new FlushResultDTO();
        long pageSize = 1000L; // 每页大小
        long currentPage = 1L; // 当前页码

        while (true) {
            Param pageable = new Param();
            pageable.setPageSize(pageSize);
            pageable.setPageNum(currentPage);
            IPage<XjSubDcpgResultDTO> pageList = xjDcpgService.findPageList(pageable, dto);

            if (pageList.getRecords().isEmpty()) {
                // 没有更多数据，退出循环
                break;
            }

            // 处理当前页的数据
            List<DcpgEntity> insertList = pageList.getRecords().stream().map(dcpgFlushConvert::convert).map(dcpgConvert::create).collect(Collectors.toList());

            processPageData(insertList, result);

            // 获取下一页数据
            currentPage++;
        }

        return result;
    }

    private void processPageData(List<DcpgEntity> insertList, FlushResultDTO flush) {

        if (CollectionUtils.isEmpty(insertList)) {
            return;
        }

        for (DcpgEntity entity : insertList) {
            try {
                dcpgService.saveOrUpdate(entity);
                flush.setSuccessCount(flush.getSuccessCount() + 1);
            } catch (Exception e) {
                flush.setErrCount(flush.getErrCount() + 1);
                flushLogService.save(flushLogConvert.convertDcpg(entity, e.getMessage()));
            }
        }
    }
}
